package com.xs.web;

import com.xs.pojo.Log;
import com.xs.service.LogSercice;
import com.xs.service.impl.LogSerciceImpl;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
    LogSercice logSercice=new LogSerciceImpl();
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理Post请求乱码问题
        request.setCharacterEncoding("utf-8");

        String id=request.getParameter("id");
        String user_name = request.getParameter("user_name");
        String sex = request.getParameter("sex");
        String home = request.getParameter("home");
        String phon = request.getParameter("phon");
        String head_time1 = request.getParameter("head_time");
        String end_time1 = request.getParameter("end_time");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date head_time = sdf.parse(head_time1);
        Date end_time = sdf.parse(end_time1);

        //封装数据
        Log log=new Log();
        log.setId(Integer.parseInt(id));
        log.setUser_name(user_name);
        log.setSex(sex);
        log.setHome(home);
        log.setPhon(phon);
        log.setHead_time(head_time);
        log.setEnd_time(end_time);

        logSercice.Update(log);
        System.out.println("/n数据修改完成");
        //转发到查询所有的serlvet
        request.getRequestDispatcher("/selectServlet").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
